//
//  PersonCenterHeaderView.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/15.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "PersonCenterHeaderView.h"

#import "UpdateUserInfoViewController.h"

#import "LoginViewController.h"

#import "MyInviteViewController.h"

#import "UserQrCodeView.h"

#import "PersonCenterViewController.h"

#import "DepositsViewController.h"

@interface PersonCenterHeaderView()<UIGestureRecognizerDelegate>

@property (nonatomic,strong) UIImageView *bgImgV;

///用户头像
@property (nonatomic,strong) UIImageView *userAvatar;
///用户昵称
@property (nonatomic,strong) UILabel *userNickNameLB;
///用户称号
@property (nonatomic,strong) UILabel *userTitleLB;
///二维码btn
@property (nonatomic,strong) UIButton *qrcodeBtn;

///用户资产view
@property (nonatomic,strong) UIView *userAssetsView;
///用户资产  余额
@property (nonatomic,strong) UILabel *userBalanceLB;
///用户资产  TB
@property (nonatomic,strong) UILabel *userTBLB;
///用户资产  TD
@property (nonatomic,strong) UILabel *userTDLB;
///邀请标题view
@property (nonatomic,strong) UIView *inviteView;
///升级成为爱心大使
@property (nonatomic,strong) UIButton *ambassadorBtn;
///升级成为工会
@property (nonatomic,strong) UIButton *unionBtn;

@end

@implementation PersonCenterHeaderView

-(id)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self initUI];
        //接受支付结果通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payResultClick:) name:@"wechatPayResultNotification" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payResultClick:) name:@"wechatPayResultNotificationSuccess" object:nil];
    }
    return self;
}
#pragma mark - noti
-(void)payResultClick:(NSNotification *)noti{
    if (object_getClassName([[Configuration getCurrentVC] class]) != object_getClassName([PersonCenterViewController class])) {
        return;
    }
    NSDictionary *dic = (NSDictionary *)noti.userInfo;
    if ([dic[@"payStatus"] intValue] == 0) {
        [[XNProgressHUD shared] showSuccessWithTitle:@"支付成功！"];
    }else{
        [[XNProgressHUD shared] showErrorWithTitle:dic[@"payResult"]];
    }
    [self updateUserInfo];
}
#pragma mark - setter
- (void)setUserInfoDict:(NSDictionary *)userInfoDict{
    _userInfoDict = userInfoDict;
    [self.userAvatar sd_setImageWithURL:[NSURL URLWithString:[_userInfoDict[@"photo"] isEmpty] == YES ? @"":_userInfoDict[@"photo"]] placeholderImage:LoadImage(@"个人中心_avatarBG")];
    self.userNickNameLB.text = [_userInfoDict[@"name"] isEmpty] == YES ? @"":_userInfoDict[@"name"];
    self.userNickNameLB.width = [Configuration getLabelWidthWithText:self.userNickNameLB.text withFont:self.userNickNameLB.font];
    self.userTitleLB.left = self.userNickNameLB.right+4;
    self.userTitleLB.text = _userInfoDict[@"type"];
    
    self.userBalanceLB.text = [NSString stringWithFormat:@"%.02f",[_userInfoDict[@"balance"] floatValue]];
    self.userTBLB.text = [NSString stringWithFormat:@"%ld",[_userInfoDict[@"credit"] integerValue]];
    
    NSInteger userIdentity = [_userInfoDict[@"identity"] integerValue];
    if (userIdentity == 1) {
        self.ambassadorBtn.hidden = NO;
        self.unionBtn.hidden = NO;
    }else if (userIdentity == 2) {
        self.unionBtn.hidden = NO;
        self.unionBtn.left = self.userNickNameLB.left;
    }
}

-(void)initUI{
    [self addSubview:self.bgImgV];
    [self addSubview:self.userAvatar];
    [self addSubview:self.userNickNameLB];
    [self addSubview:self.ambassadorBtn];
    [self addSubview:self.unionBtn];
    [self addSubview:self.qrcodeBtn];
    
    self.userNickNameLB.text = @"点击登录";
    self.userNickNameLB.width = [Configuration getLabelWidthWithText:self.userNickNameLB.text withFont:self.userNickNameLB.font];
    
    [self addSubview:self.userTitleLB];
    
    [self initUserAssetsView];
    
    [self initInviteView];
}

//加载用户资产view
-(void)initUserAssetsView{
    [self addSubview:self.userAssetsView];
    
    float imgInterval = (self.userAssetsView.width-20)/3;
    
    NSArray *imgs = @[@"个人中心_余额",@"个人中心_TB",@"个人中心_TD"];
    NSArray *moneys = @[@"0.00",@"0",@"0"];
    NSArray *units = @[@"余额",@"TB(个)",@"TD(个)"];
    for (int i = 0; i<3; i++) {
        UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(imgInterval-36 + i*imgInterval, 22, 36, 36)];
        imgV.image = LoadImage(imgs[i]);
        [self.userAssetsView addSubview:imgV];
        
        UILabel *moneyLB = [[UILabel alloc] initWithFrame:CGRectMake(5+i*imgInterval, 20, imgInterval-46, 24)];
        moneyLB.textAlignment = NSTextAlignmentCenter;
        moneyLB.font = FontSet(15);
        moneyLB.textColor = RGB(248, 86, 0);
        moneyLB.text = moneys[i];
        [self.userAssetsView addSubview:moneyLB];
        
        UILabel *unitLB = [[UILabel alloc] initWithFrame:CGRectMake(15+i*imgInterval, moneyLB.bottom+2, imgInterval-56, 24)];
        unitLB.textAlignment = NSTextAlignmentCenter;
        unitLB.font = FontSetWithWeight(14, 0.3f);
        unitLB.textColor = MainBlackTextColor;
        unitLB.text = units[i];
        [self.userAssetsView addSubview:unitLB];
        
        if (i == 0) {
            self.userBalanceLB = moneyLB;
            UIView *balanceMengBanView = [[UIView alloc] initWithFrame:CGRectMake(5, 20, imgInterval-10, 40)];
            balanceMengBanView.backgroundColor = Color_Clear;
            balanceMengBanView.userInteractionEnabled = YES;
            [self.userAssetsView addSubview:balanceMengBanView];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewBalanceClick:)];
            tap.delegate = self;
            [balanceMengBanView addGestureRecognizer:tap];
            
        }else if (i == 1) {
            self.userTBLB = moneyLB;
        }else{
            self.userTDLB = moneyLB;
        }
    }
}
//邀请标题view
-(void)initInviteView{
    
    self.inviteView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-40, ScreenWidth, 25)];
    self.inviteView.backgroundColor = Color_Clear;
    [self addSubview:self.inviteView];
    self.inviteView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(inviteTapClick:)];
    tap.delegate = self;
    [self.inviteView addGestureRecognizer:tap];
    
    UILabel *leftLB = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 100, 25)];
    leftLB.textColor = MainBlackTextColor;
    leftLB.font = FontSetWithWeight(18, 0.3f);
    leftLB.text = @"我的邀请";
    [self.inviteView addSubview:leftLB];
    
    UILabel *detailLB = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 200, leftLB.top+3, 166, 18)];
    detailLB.textColor = MainPureColor(153);
    detailLB.font = FontSet(13);
    detailLB.text = @"查看全部";
    detailLB.textAlignment = NSTextAlignmentRight;
//    [self.inviteView addSubview:detailLB];
    
    UIImageView *moreImgV = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 30, detailLB.top+3, 6, 12)];
    moreImgV.image = LoadImage(@"个人中心_查看全部");
//    [self.inviteView addSubview:moreImgV];
}


#pragma mark - lazy
-(UIImageView *)bgImgV{
    if (!_bgImgV) {
        _bgImgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, self.height)];
        _bgImgV.image = LoadImage(@"个人中心_背景");
    }
    return _bgImgV;
}

-(UIImageView *)userAvatar{
    if (!_userAvatar) {
        _userAvatar = [[UIImageView alloc] initWithFrame:CGRectMake(25, kHeight_NavBar, 54, 54)];
        _userAvatar.image = LoadImage(@"个人中心_avatarBG");
        _userAvatar.userInteractionEnabled = YES;
        _userAvatar.clipsToBounds = YES;
        _userAvatar.layer.cornerRadius = 27;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        tap.delegate = self;
        [_userAvatar addGestureRecognizer:tap];
    }
    return _userAvatar;
}

-(UILabel *)userNickNameLB{
    if (!_userNickNameLB) {
        _userNickNameLB = [[UILabel alloc] initWithFrame:CGRectMake(self.userAvatar.right+8, self.userAvatar.top+3, 50, 25)];
        _userNickNameLB.font = FontSet(18);
        _userNickNameLB.textColor = MainPureColor(255);
        _userNickNameLB.textAlignment = NSTextAlignmentLeft;
        _userNickNameLB.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        tap.delegate = self;
        [_userNickNameLB addGestureRecognizer:tap];
    }
    return _userNickNameLB;
}
- (UIButton *)qrcodeBtn{
    if (!_qrcodeBtn) {
        _qrcodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _qrcodeBtn.frame = CGRectMake(ScreenWidth-45, self.userNickNameLB.top, 30, 30);
        [_qrcodeBtn setImage:LoadImage(@"个人中心_二维码") forState:normal];
        [_qrcodeBtn addTarget:self action:@selector(qrcodeDetailClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _qrcodeBtn;
}

-(UILabel *)userTitleLB{
    if (!_userTitleLB) {
        _userTitleLB = [[UILabel alloc] initWithFrame:CGRectMake(self.userNickNameLB.right+4, self.userNickNameLB.top+3, 54, 18)];
        _userTitleLB.backgroundColor = UIColor.whiteColor;
        _userTitleLB.clipsToBounds = YES;
        _userTitleLB.layer.cornerRadius = 9;
        _userTitleLB.font = FontSet(10);
        _userTitleLB.textColor = RGB(77, 140, 235);
        _userTitleLB.textAlignment = NSTextAlignmentCenter;
        _userTitleLB.text = @"普通会员";
        
    }
    return _userTitleLB;
}
- (UIButton *)ambassadorBtn{
    if (!_ambassadorBtn) {
        _ambassadorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _ambassadorBtn.frame = CGRectMake(self.userNickNameLB.left, self.userNickNameLB.bottom+7, 120, 20);
        [_ambassadorBtn.titleLabel setFont:[UIFont systemFontOfSize:11]];
        [_ambassadorBtn setTitle:@"升级成为爱心大使" forState:normal];
        [_ambassadorBtn setImage:LoadImage(@"个人中心_爱心大使") forState:normal];
        [_ambassadorBtn setImageEdgeInsets:UIEdgeInsetsMake(2, 0, 2, 104)];
        [_ambassadorBtn addTarget:self action:@selector(upgradeToAmbassadorClick) forControlEvents:UIControlEventTouchUpInside];
        _ambassadorBtn.hidden = YES;
    }
    return _ambassadorBtn;
}
- (UIButton *)unionBtn{
    if (!_unionBtn) {
        _unionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _unionBtn.frame = CGRectMake(self.ambassadorBtn.right + 5, self.userNickNameLB.bottom+7, 100, 20);
        [_unionBtn.titleLabel setFont:[UIFont systemFontOfSize:11]];
        [_unionBtn setTitle:@"升级成为公会" forState:normal];
        [_unionBtn setImage:LoadImage(@"个人中心_公会") forState:normal];
        [_unionBtn setImageEdgeInsets:UIEdgeInsetsMake(2, 0, 2, 84)];
        [_unionBtn addTarget:self action:@selector(upgradeToUnionClick) forControlEvents:UIControlEventTouchUpInside];
        _unionBtn.hidden = YES;
    }
    return _unionBtn;
}

//用户资产view
-(UIView *)userAssetsView{
    if (!_userAssetsView) {
        _userAssetsView = [[UIView alloc] initWithFrame:CGRectMake(10, self.userAvatar.bottom+18, ScreenWidth-20, 80)];
        _userAssetsView.backgroundColor = UIColor.whiteColor;
        _userAssetsView.clipsToBounds = YES;
        _userAssetsView.layer.cornerRadius = 8;
    }
    return _userAssetsView;
}

#pragma mark - SEL
-(void)tapClick:(UIGestureRecognizer *)ges{
    
    if ([UserInfomationManager shareManager].loginStatus == NO) {
        [self loginClick];
        return;
    }
    
    UpdateUserInfoViewController *vc = [[UpdateUserInfoViewController alloc] init];
    vc.userInfoDict = self.userInfoDict;
    [[Configuration getCurrentVC].navigationController pushViewController:vc animated:YES];
}


//去登陆
-(void)loginClick{
    LoginViewController *vc = [[LoginViewController alloc] init];
    vc.modalPresentationStyle = 0;
    [[Configuration getCurrentVC] presentViewController:vc animated:YES completion:nil];
}

-(void)inviteTapClick:(UIGestureRecognizer *)ges{
    if ([UserInfomationManager shareManager].loginStatus == NO) {
//        [self loginClick];
        return;
    }
}
//查看二维码
-(void)qrcodeDetailClick{
    if ([UserInfomationManager shareManager].loginStatus == NO) {
        [self loginClick];
        return;
    }
    UserQrCodeView *view = [[UserQrCodeView alloc] init];
    [view show];
}
//升级成为爱心大使
-(void)upgradeToAmbassadorClick{
    if ([UserInfomationManager shareManager].loginStatus == NO) {
        [self loginClick];
        return;
    }
    [OrderManager showCustomAlertTitle:@"升级成为爱心大使!" message:@"确定升级成为爱心大使吗？" confirmBlock:^{
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"source"] = @"app";
        params[@"type"] = @(2);
        [NetWorkingPerson userUpgradeWithParams:params WithSuccessBlock:^(id  _Nonnull responseObj) {
            NSDictionary *payDict = responseObj[@"data"];
            [WechatPayManager handleWechatPayWithPayDic:payDict];
        }];
    }];
}
//升级成为工会
-(void)upgradeToUnionClick{
    if ([UserInfomationManager shareManager].loginStatus == NO) {
        [self loginClick];
        return;
    }
    [OrderManager showCustomAlertTitle:@"升级成为公会!" message:@"确定升级成为公会吗？" confirmBlock:^{
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"source"] = @"app";
        params[@"type"] = @(3);
        [NetWorkingPerson userUpgradeWithParams:params WithSuccessBlock:^(id  _Nonnull responseObj) {
            NSDictionary *payDict = responseObj[@"data"];
            [WechatPayManager handleWechatPayWithPayDic:payDict];
        }];
    }];
}

-(void)updateUserInfo{
    if (self.updateUserInfoBlock) {
        self.updateUserInfoBlock();
    }
}

//查看余额  提现
-(void)viewBalanceClick:(UIGestureRecognizer *)ges{
    if ([UserInfomationManager shareManager].loginStatus == NO) {
        [self loginClick];
        return;
    }
    DepositsViewController *vc = [[DepositsViewController alloc] init];
    [[Configuration getCurrentVC].navigationController pushViewController:vc animated:YES];
}


@end
